import { getOrderList } from '@/api/order.js'
import { getListData } from '@/api/shop/trade.js'
import { getData } from '@/api/discountCoupon/universal.js'
import { getUserList, getShopList } from '@/api/order.js'

export default {
  namespaced: true,
  state: {
    orderId: '',
    showAddDiglog: false,
    showOrderDetail: false,
    handleOrderData: [],
    shopnamelist: [],
    userlist: [],
    couponlist: [],
    fullUserList: [],
    goodDataList: [],
    shopDataList: [],
    page: {
      page: 1,
      size: 10,
      total: 0
    },
    phonelist: [],
    orderlist: []
  },
  mutations: {
    saveOrder(state, load) {
      const newArr = []
      const newArr1 = []
      state.page.total = load.total
      load.data.forEach(value => {
        newArr.push(value.id)
        if (value.ctct_tel !== '' && newArr1.indexOf(value.ctct_tel) === -1) {
          newArr1.push(value.ctct_tel)
        }
      })
      state.orderlist = newArr
      state.phonelist = newArr1
      state.handleOrderData = load.data
    },
    saveShop(state, data) {
      var newArr2 = []
      data.forEach(value => {
        if (newArr2.indexOf(value.shop_name.trim()) === -1) {
          newArr2.push(value.shop_name)
          state.shopDataList.push(value)
        }
      })
      state.shopnamelist = newArr2
    },
    saveCoupon(state, data) {
      var newArr = []
      data.forEach(value => {
        if (newArr.indexOf(value.name) === -1) {
          newArr.push(value.name)
        }
      })
      state.couponlist = newArr
    },
    saveUser(state, data) {
      var newArr1 = []
      data.forEach(value => {
        if (newArr1.indexOf(value.username) === -1) {
          state.fullUserList.push(value)
          newArr1.push(value.username)
        }
      })

      state.userlist = newArr1
    },
    saveShopList(state, data) {
      state.goodDataList = data
    }
  },
  actions: {
    // 获取商铺列表
    getShopData(context, data) {
      return getListData().then((res) => {
        context.commit('saveShop', res.data)
      }).catch(() => {
        return Promise.reject()
      })
    },
    // 获取订单列表
    getOrderData(context, data) {
      return getOrderList().then((res) => {
        context.commit('saveOrder', res)
      }).catch(() => {
        return Promise.reject()
      })
    },
    // 获取优惠券列表
    getCouponData(context, data) {
      return getData().then((res) => {
        context.commit('saveCoupon', res.data)
      }).catch(() => {
        return Promise.reject()
      })
    },
    // 获取用户列表
    getUserData(context, data) {
      return getUserList().then((res) => {
        context.commit('saveUser', res.data)
      }).catch(() => {
        return Promise.reject()
      })
    },
    // 获取商品列表
    getShopDataList(context, data) {
      return getShopList().then((res) => {
        context.commit('saveShopList', res.data)
        return res.total
      }).catch(() => {
        return Promise.reject()
      })
    }
  }
}
